import os
import glob
import json

def load_custom_palettes():
    """动态加载CustomPalette目录中的所有JSON预设"""
    custom_palettes = {}
    palette_dir = os.path.join(os.path.dirname(__file__), "CustomPalette")
    
    if os.path.exists(palette_dir):
        for json_file in glob.glob(os.path.join(palette_dir, "*.json")):
            try:
                with open(json_file, 'r', encoding='utf-8') as f:
                    palette_name = os.path.splitext(os.path.basename(json_file))[0]
                    colors = json.load(f)
                    # 重新生成ID以避免冲突
                    for i, color in enumerate(colors):
                        color["id"] = i
                    custom_palettes[palette_name] = colors
            except Exception as e:
                print(f"Error loading palette {json_file}: {e}")
    
    return custom_palettes

def refresh_custom_palettes():
    """重新加载自定义调色板并更新全局预设"""
    global COLORS_PRESETS
    custom_palettes = load_custom_palettes()
    COLORS_PRESETS.update(custom_palettes)

# 合并预设颜色和自定义调色板
COLORS_PRESETS = {
    "五色": [
        {"name": "青", "color": (0, 153/255, 153/255, 1.0)},
        {"name": "赤", "color": (255/255, 0, 0, 1.0)},
        {"name": "黄", "color": (255/255, 255/255, 0, 1.0)},
        {"name": "白", "color": (255/255, 255/255, 255/255, 1.0)},
        {"name": "黑", "color": (0, 0, 0, 1.0)}
    ],
    "WEB标准色": [
        {"name": "爱丽丝蓝", "color": (240/255, 248/255, 255/255, 1.0)},
        {"name": "古董白", "color": (250/255, 235/255, 215/255, 1.0)},
        {"name": "碧绿", "color": (127/255, 255/255, 212/255, 1.0)},
        {"name": "青白色", "color": (240/255, 255/255, 255/255, 1.0)},
        {"name": "米色", "color": (245/255, 245/255, 220/255, 1.0)},
        {"name": "陶坯黄", "color": (255/255, 228/255, 196/255, 1.0)},
        {"name": "黑色", "color": (0/255, 0/255, 0/255, 1.0)},
        {"name": "杏仁白", "color": (255/255, 235/255, 205/255, 1.0)},
        {"name": "蓝色", "color": (0/255, 0/255, 255/255, 1.0)},
        {"name": "蓝紫色", "color": (138/255, 43/255, 226/255, 1.0)},
        {"name": "褐色", "color": (165/255, 42/255, 42/255, 1.0)},
        {"name": "硬木褐", "color": (222/255, 184/255, 135/255, 1.0)},
        {"name": "军服蓝", "color": (95/255, 158/255, 160/255, 1.0)},
        {"name": "查特酒绿", "color": (127/255, 255/255, 0/255, 1.0)},
        {"name": "巧克力色", "color": (210/255, 105/255, 30/255, 1.0)},
        {"name": "珊瑚红", "color": (255/255, 127/255, 80/255, 1.0)},
        {"name": "矢车菊蓝", "color": (100/255, 149/255, 237/255, 1.0)},
        {"name": "玉米穗黄", "color": (255/255, 248/255, 220/255, 1.0)},
        {"name": "绯红", "color": (220/255, 20/255, 60/255, 1.0)},
        {"name": "青色", "color": (0/255, 255/255, 255/255, 1.0)},
        {"name": "深蓝", "color": (0/255, 0/255, 139/255, 1.0)},
        {"name": "深青", "color": (0/255, 139/255, 139/255, 1.0)},
        {"name": "深金菊黄", "color": (184/255, 134/255, 11/255, 1.0)},
        {"name": "暗灰", "color": (169/255, 169/255, 169/255, 1.0)},
        {"name": "深绿", "color": (0/255, 100/255, 0/255, 1.0)},
        {"name": "深卡其色", "color": (189/255, 183/255, 107/255, 1.0)},
        {"name": "深品红", "color": (139/255, 0/255, 139/255, 1.0)},
        {"name": "深橄榄绿", "color": (85/255, 107/255, 47/255, 1.0)},
        {"name": "深橙", "color": (255/255, 140/255, 0/255, 1.0)},
        {"name": "深洋兰紫", "color": (153/255, 50/255, 204/255, 1.0)},
        {"name": "深红", "color": (139/255, 0/255, 0/255, 1.0)},
        {"name": "深鲑红", "color": (233/255, 150/255, 122/255, 1.0)},
        {"name": "深海藻绿", "color": (143/255, 188/255, 143/255, 1.0)},
        {"name": "深岩蓝", "color": (72/255, 61/255, 139/255, 1.0)},
        {"name": "深岩灰", "color": (47/255, 79/255, 79/255, 1.0)},
        {"name": "深松石绿", "color": (0/255, 206/255, 209/255, 1.0)},
        {"name": "深紫", "color": (148/255, 0/255, 211/255, 1.0)},
        {"name": "深粉", "color": (255/255, 20/255, 147/255, 1.0)},
        {"name": "深天蓝", "color": (0/255, 191/255, 255/255, 1.0)},
        {"name": "昏灰", "color": (105/255, 105/255, 105/255, 1.0)},
        {"name": "湖蓝", "color": (30/255, 144/255, 255/255, 1.0)},
        {"name": "火砖红", "color": (178/255, 34/255, 34/255, 1.0)},
        {"name": "花卉白", "color": (255/255, 250/255, 240/255, 1.0)},
        {"name": "森林绿", "color": (34/255, 139/255, 34/255, 1.0)},
        {"name": "庚氏灰", "color": (220/255, 220/255, 220/255, 1.0)},
        {"name": "幽灵白", "color": (248/255, 248/255, 255/255, 1.0)},
        {"name": "金色", "color": (255/255, 215/255, 0/255, 1.0)},
        {"name": "金菊黄", "color": (218/255, 165/255, 32/255, 1.0)},
        {"name": "灰色", "color": (128/255, 128/255, 128/255, 1.0)},
        {"name": "绿色", "color": (0/255, 128/255, 0/255, 1.0)},
        {"name": "黄绿色", "color": (173/255, 255/255, 47/255, 1.0)},
        {"name": "蜜瓜绿", "color": (240/255, 255/255, 240/255, 1.0)},
        {"name": "艳粉", "color": (255/255, 105/255, 180/255, 1.0)},
        {"name": "印度红", "color": (205/255, 92/255, 92/255, 1.0)},
        {"name": "靛蓝", "color": (75/255, 0/255, 130/255, 1.0)},
        {"name": "象牙白", "color": (255/255, 255/255, 240/255, 1.0)},
        {"name": "卡其色", "color": (240/255, 230/255, 140/255, 1.0)},
        {"name": "薰衣草紫", "color": (230/255, 230/255, 250/255, 1.0)},
        {"name": "薰衣草红", "color": (255/255, 240/255, 245/255, 1.0)},
        {"name": "草坪绿", "color": (124/255, 252/255, 0/255, 1.0)},
        {"name": "柠檬绸黄", "color": (255/255, 250/255, 205/255, 1.0)},
        {"name": "浅蓝", "color": (173/255, 216/255, 230/255, 1.0)},
        {"name": "浅珊瑚红", "color": (240/255, 128/255, 128/255, 1.0)},
        {"name": "浅青", "color": (224/255, 255/255, 255/255, 1.0)},
        {"name": "浅金菊黄", "color": (250/255, 250/255, 210/255, 1.0)},
        {"name": "亮灰", "color": (211/255, 211/255, 211/255, 1.0)},
        {"name": "浅绿", "color": (144/255, 238/255, 144/255, 1.0)},
        {"name": "浅粉", "color": (255/255, 182/255, 193/255, 1.0)},
        {"name": "浅鲑红", "color": (255/255, 160/255, 122/255, 1.0)},
        {"name": "浅海藻绿", "color": (32/255, 178/255, 170/255, 1.0)},
        {"name": "浅天蓝", "color": (135/255, 206/255, 250/255, 1.0)},
        {"name": "浅岩灰", "color": (119/255, 136/255, 153/255, 1.0)},
        {"name": "亮钢蓝", "color": (176/255, 196/255, 222/255, 1.0)},
        {"name": "浅黄", "color": (255/255, 255/255, 224/255, 1.0)},
        {"name": "绿色", "color": (0/255, 255/255, 0/255, 1.0)},
        {"name": "青柠绿", "color": (50/255, 205/255, 50/255, 1.0)},
        {"name": "亚麻色", "color": (250/255, 240/255, 230/255, 1.0)},
        {"name": "洋红", "color": (255/255, 0/255, 255/255, 1.0)},
        {"name": "栗色", "color": (128/255, 0/255, 0/255, 1.0)},
        {"name": "中碧绿", "color": (102/255, 205/255, 170/255, 1.0)},
        {"name": "中蓝", "color": (0/255, 0/255, 205/255, 1.0)},
        {"name": "中洋兰紫", "color": (186/255, 85/255, 211/255, 1.0)},
        {"name": "中紫", "color": (147/255, 112/255, 219/255, 1.0)},
        {"name": "中海藻绿", "color": (60/255, 179/255, 113/255, 1.0)},
        {"name": "中岩蓝", "color": (123/255, 104/255, 238/255, 1.0)},
        {"name": "中嫩绿", "color": (0/255, 250/255, 154/255, 1.0)},
        {"name": "中松石绿", "color": (72/255, 209/255, 204/255, 1.0)},
        {"name": "中紫红", "color": (199/255, 21/255, 133/255, 1.0)},
        {"name": "午夜蓝", "color": (25/255, 25/255, 112/255, 1.0)},
        {"name": "薄荷乳白", "color": (245/255, 255/255, 250/255, 1.0)},
        {"name": "雾玫瑰红", "color": (255/255, 228/255, 225/255, 1.0)},
        {"name": "鹿皮色", "color": (255/255, 228/255, 181/255, 1.0)},
        {"name": "土著白", "color": (255/255, 222/255, 173/255, 1.0)},
        {"name": "藏青", "color": (0/255, 0/255, 128/255, 1.0)},
        {"name": "旧蕾丝白", "color": (253/255, 245/255, 230/255, 1.0)},
        {"name": "橄榄色", "color": (128/255, 128/255, 0/255, 1.0)},
        {"name": "橄榄绿", "color": (107/255, 142/255, 35/255, 1.0)},
        {"name": "橙色", "color": (255/255, 165/255, 0/255, 1.0)},
        {"name": "橘红", "color": (255/255, 69/255, 0/255, 1.0)},
        {"name": "洋兰紫", "color": (218/255, 112/255, 214/255, 1.0)},
        {"name": "白金菊黄", "color": (238/255, 232/255, 170/255, 1.0)},
        {"name": "白绿色", "color": (152/255, 251/255, 152/255, 1.0)},
        {"name": "白松石绿", "color": (175/255, 238/255, 238/255, 1.0)},
        {"name": "弱紫罗兰红", "color": (219/255, 112/255, 147/255, 1.0)},
        {"name": "番木瓜橙", "color": (255/255, 239/255, 213/255, 1.0)},
        {"name": "粉扑桃色", "color": (255/255, 218/255, 185/255, 1.0)},
        {"name": "秘鲁红", "color": (205/255, 133/255, 63/255, 1.0)},
        {"name": "粉色", "color": (255/255, 192/255, 203/255, 1.0)},
        {"name": "李紫", "color": (221/255, 160/255, 221/255, 1.0)},
        {"name": "粉末蓝", "color": (176/255, 224/255, 230/255, 1.0)},
        {"name": "紫色", "color": (128/255, 0/255, 128/255, 1.0)},
        {"name": "红色", "color": (255/255, 0/255, 0/255, 1.0)},
        {"name": "玫瑰褐", "color": (188/255, 143/255, 143/255, 1.0)},
        {"name": "品蓝", "color": (65/255, 105/255, 225/255, 1.0)},
        {"name": "鞍褐", "color": (139/255, 69/255, 19/255, 1.0)},
        {"name": "鲑红", "color": (250/255, 128/255, 114/255, 1.0)},
        {"name": "沙褐", "color": (244/255, 164/255, 96/255, 1.0)},
        {"name": "海藻绿", "color": (46/255, 139/255, 87/255, 1.0)},
        {"name": "贝壳白", "color": (255/255, 245/255, 238/255, 1.0)},
        {"name": "土黄赭", "color": (160/255, 82/255, 45/255, 1.0)},
        {"name": "银色", "color": (192/255, 192/255, 192/255, 1.0)},
        {"name": "天蓝", "color": (135/255, 206/255, 235/255, 1.0)},
        {"name": "岩蓝", "color": (106/255, 90/255, 205/255, 1.0)},
        {"name": "岩灰", "color": (112/255, 128/255, 144/255, 1.0)},
        {"name": "雪白", "color": (255/255, 250/255, 250/255, 1.0)},
        {"name": "春绿", "color": (0/255, 255/255, 127/255, 1.0)},
        {"name": "钢青", "color": (70/255, 130/255, 180/255, 1.0)},
        {"name": "日晒褐", "color": (210/255, 180/255, 140/255, 1.0)},
        {"name": "海天蓝", "color": (135/255, 206/255, 235/255, 1.0)},
        {"name": "鸭翅绿", "color": (0/255, 128/255, 128/255, 1.0)},
        {"name": "蓟紫", "color": (216/255, 191/255, 216/255, 1.0)},
        {"name": "番茄红", "color": (255/255, 99/255, 71/255, 1.0)},
        {"name": "松石绿", "color": (64/255, 224/255, 208/255, 1.0)},
        {"name": "紫罗兰色", "color": (238/255, 130/255, 238/255, 1.0)},
        {"name": "麦色", "color": (245/255, 222/255, 179/255, 1.0)},
        {"name": "白色", "color": (255/255, 255/255, 255/255, 1.0)},
        {"name": "烟雾白", "color": (245/255, 245/255, 245/255, 1.0)},
        {"name": "黄色", "color": (255/255, 255/255, 0/255, 1.0)},
        {"name": "暗黄绿色", "color": (154/255, 205/255, 50/255, 1.0)},
        {"name": "金属灰", "color": (0.5, 0.5, 0.5, 1.0)},
        {"name": "机械蓝", "color": (0.2, 0.3, 0.7, 1.0)},
        {"name": "工业红", "color": (0.8, 0.2, 0.2, 1.0)},
        {"name": "森林绿", "color": (34/255, 139/255, 34/255, 1.0)},
        {"name": "天空蓝", "color": (135/255, 206/255, 235/255, 1.0)},
        {"name": "土壤褐", "color": (139/255, 69/255, 19/255, 1.0)}
    ]
}

# 动态加载自定义调色板并合并到预设中
custom_palettes = load_custom_palettes()
COLORS_PRESETS.update(custom_palettes)
